package com.rocksea.app.tasks;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.rocksea.app.db.DbCenter;
import com.rocksea.app.db.HighStrain.HighStrain;
import com.rocksea.app.db.SonicWave.SonicWave;
import com.rocksea.app.db.StaticLoad.StaticLoad;
import com.rocksea.app.db.history.HistoryTable;
import com.rocksea.app.db.lowStrain.LowStrainDb;
import com.rocksea.app.entry.HighStarinDb;
import com.rocksea.app.entry.LoadDataEvent;
import com.rocksea.app.interfaces.MyExecuteCallBack;
import com.rocksea.app.utils.PerUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 吴志华 on 2017/10/26.
 *
 * @FileName:com.rocksea.app.tasks.QueryPileTask.java
 */

public class QueryPileTask extends AsyncTask<Void,Void,String[]> {
    public static final int ERROR_CODE = -1;

    public static final String LABLE = "&";
    private String baseInfoId;
    private MyExecuteCallBack<String[]> myExecuteCallBack;

    public QueryPileTask() {
    }

    public QueryPileTask(String baseInfoId, MyExecuteCallBack<String[]> myExecuteCallBack)
    {
        this.myExecuteCallBack = myExecuteCallBack;
        this.baseInfoId = baseInfoId;
    }
    @Override
    protected String[] doInBackground(Void... params) {
        List<HistoryTable> historyTable = DbCenter.mHistoryDbHelper.queryByBaseInfoId(baseInfoId);
        if (historyTable==null||historyTable.size()==0)
            return new String[0];
        return dropInfoToArray(historyTable.get(0));
    }
    private String[] dropInfoToArray(HistoryTable historyTable)
    {
        ArrayList<String> infos = new ArrayList<>();
        switch (historyTable.getType())
        {
            case HistoryTable.TEST_DY:
                LowStrainDb mLowStrain = DbCenter.mDyDbHelper.queryByBaseInfoId(historyTable.BaseInfoId);
                if (mLowStrain!=null)
                {
                    Temp.setLowStrainDb(mLowStrain);
                    commonOption(historyTable, infos);
                    infos.add(String.format("仪器编号%s%s",LABLE,String.valueOf(mLowStrain.machineId)));
                    infos.add(String.format("桩长%s%sm",LABLE,String.valueOf(mLowStrain.pileLength)));
                    infos.add(String.format("桩径%s%smm",LABLE,String.valueOf(mLowStrain.pileDiameter)));
                    infos.add(String.format("预设波速%s%sm/s",LABLE,String.valueOf(mLowStrain.velocity)));
                    infos.add(String.format("采样数量%s%s",LABLE,String.valueOf(mLowStrain.sampleNumber)));
                    if(mLowStrain.GpsIsValid==0)
                        infos.add(String.format("GPS有效性%s%s",LABLE,"有效"));
                    else
                        infos.add(String.format("GPS有效性%s%s",LABLE,"无效"));
                }
                break;

            case HistoryTable.TEST_GY:
                HighStrain mHighStrain = DbCenter.mGyDbHelper.queryByBaseInfoId(historyTable.BaseInfoId);
                commonOption(historyTable, infos);
                if (mHighStrain != null)
                {
                    Temp.setHighStrain(mHighStrain);
                    HighStarinDb mHighStarinDb = JSON.parseObject(mHighStrain.sourceData, HighStarinDb.class);
                    infos.add(String.format("仪器编号%s%s",LABLE,String.valueOf(mHighStrain.machineId)));
                    infos.add(String.format("桩长%s%sm",LABLE,String.valueOf(mHighStarinDb.HammerData.get(0).PileLength)));
                    infos.add(String.format("桩径%s%smm",LABLE,String.valueOf(mHighStarinDb.HammerData.get(0).PileDiameter)));
                    infos.add(String.format("总锤数%s%smm",LABLE,String.valueOf(mHighStarinDb.TotalHammers)));
                    if(mHighStrain.GpsIsValid == 0)
                        infos.add(String.format("GPS有效性%s%s",LABLE,"有效"));
                    else
                        infos.add(String.format("GPS有效性%s%s",LABLE,"无效"));
                }
                break;

            case HistoryTable.TEST_JZ:
                StaticLoad mStaticLoad = DbCenter.mStaticLoadDbHelper.queryById(historyTable.BaseInfoId);
                if (mStaticLoad!=null)
                {
                    Temp.setStaticLoad(mStaticLoad);
                    commonOption(historyTable,infos);
                    infos.remove(3);
                    infos.add(String.format("仪器编号%s%s",LABLE,String.valueOf(mStaticLoad.machineId)));
                    if (TextUtils.isEmpty(mStaticLoad.standard))
                        infos.add(String.format("测试规范%s%s",LABLE,String.valueOf(mStaticLoad.testType)));
                    else
                    {
                        String standard = mStaticLoad.standard;
                        if (standard.length() > 22)
                        {
                            String text = String.format("%s...%s",standard.substring(0,3),
                                    standard.substring(standard.length() - 3,standard.length()));
                            infos.add(String.format("测试规范%s%s",LABLE,text));
                        }
                        else
                            infos.add(String.format("测试规范%s%s",LABLE,String.valueOf(mStaticLoad.standard)));
                    }
                    infos.add(String.format("设计载荷%s%sKN",LABLE,String.valueOf(mStaticLoad.maxLoad)));
                    if (mStaticLoad.boardArea<   0)
                        infos.add(String.format("压板面积%s%s㎡",LABLE,String.valueOf(1.0)));
                    else
                        infos.add(String.format("压板面积%s%s㎡",LABLE,String.valueOf(mStaticLoad.boardArea)));
                    infos.add(String.format("分级数%s%s",LABLE,String.valueOf(mStaticLoad.preSeries)));
                    infos.add(String.format("最大预估荷载%s%sKN",LABLE,String.valueOf(mStaticLoad.maxLoad)));
                    String sensorId = mStaticLoad.sensorID.replace("λ",",");
                    infos.add(String.format("位移传感器编号%s%s",LABLE,sensorId));
                    infos.add(String.format("压力传感器编号%s%s",LABLE,String.valueOf(mStaticLoad.pressID)));
                    if(mStaticLoad.GpsIsValid==0)
                        infos.add(String.format("GPS有效性%s%s",LABLE,"有效"));
                    else
                        infos.add(String.format("GPS有效性%s%s",LABLE,"无效"));
                }
                break;

            case HistoryTable.TEST_SC:
                SonicWave mSonicWave = DbCenter.mScDbHelper.querySonicWaveFragmet(historyTable.BaseInfoId);
                if (mSonicWave!=null)
                {
                    Temp.setSonicWave(mSonicWave);
                    commonOption(historyTable, infos);
                    infos.add(String.format("仪器编号%s%s",LABLE,String.valueOf(mSonicWave.machineId)));
                    infos.add(String.format("桩长%s%sm",LABLE,String.valueOf(mSonicWave.pileLength)));
                    infos.add(String.format("桩径%s%smm",LABLE,String.valueOf(mSonicWave.pileDiameter)));
                    infos.add(String.format("管数%s%s",LABLE,String.valueOf(mSonicWave.totalPipe)));
                    infos.add(String.format("移距%s%smm",LABLE,String.valueOf(mSonicWave.step)));
                    infos.add(String.format("采样间隔%s%s",LABLE,String.valueOf(mSonicWave.sampleInterval)));
                    infos.add(String.format("采样点数%s%s",LABLE,String.valueOf(mSonicWave.samplePoints)));
                    infos.add(String.format("剖面数%s%s",LABLE,String.valueOf(mSonicWave.SonicWaveFaces.size())));
                    if(mSonicWave.GpsIsValid==0)
                        infos.add(String.format("GPS有效性%s%s",LABLE,"有效"));
                    else
                        infos.add(String.format("GPS有效性%s%s",LABLE,"无效"));
                }
                break;
        }

        if (infos.size()==0)
            return null;
        else
        {
            String[] infoArrays = new String[infos.size()];
            for (int i = 0; i < infos.size(); i++)
            {
                infoArrays[i] = infos.get(i);
            }
            return infoArrays;
        }
    }

    private void commonOption(HistoryTable historyTable, ArrayList<String> infos) {
        infos.add(String.format("流水号%s%s",LABLE,historyTable.getSerialNo()));
        infos.add(String.format("桩号%s%s",LABLE,historyTable.getPileNo()));
        infos.add(String.format("测试时间%s%s",LABLE, PerUtils.dateToStr(new Date(historyTable.CreatTime))));
        if (historyTable.getIsLoad() == 0)
        {
            infos.add(String.format("上传时间%s%s",LABLE,"未上传"));
        }
        else
        {
            infos.add(String.format("上传时间%s%s",LABLE,PerUtils.dateToStr(new Date(historyTable.LoadTime))));
        }
    }
    @Override
    protected void onPostExecute(String[] strings) {
        super.onPostExecute(strings);
        if (strings==null)
        {
            myExecuteCallBack.onError(ERROR_CODE,"获取数据失败");
            return;
        }
        myExecuteCallBack.onSuccess(strings);
        EventBus.getDefault().post(new LoadDataEvent());
    }


    public String[] getSonicWaveBaseInfo(SonicWave mSonicWave)
    {
        ArrayList<String> infos = new ArrayList<>();
        infos.add(String.format("流水号%s%s",LABLE,mSonicWave.serialNo));
        infos.add(String.format("桩号%s%s",LABLE,mSonicWave.pileNo));
        infos.add(String.format("测试时间%s%s",LABLE, mSonicWave.startTime));
        if (mSonicWave.upLoadTime>0)
        {
            infos.add(String.format("上传时间%s%s",LABLE,PerUtils.dateToStr(new Date(mSonicWave.upLoadTime))));
        }
        else
        {
            infos.add(String.format("上传时间%s%s",LABLE,"未上传"));
        }
        infos.add(String.format("仪器编号%s%s",LABLE,String.valueOf(mSonicWave.machineId)));
        infos.add(String.format("桩长%s%sm",LABLE,String.valueOf(mSonicWave.pileLength)));
        infos.add(String.format("桩径%s%smm",LABLE,String.valueOf(mSonicWave.pileDiameter)));
        infos.add(String.format("管数%s%s",LABLE,String.valueOf(mSonicWave.totalPipe)));
        infos.add(String.format("移距%s%smm",LABLE,String.valueOf(mSonicWave.step)));
        infos.add(String.format("采样间隔%s%sus",LABLE,String.valueOf(mSonicWave.sampleInterval)));
        infos.add(String.format("采样点数%s%s",LABLE,String.valueOf(mSonicWave.samplePoints)));
        //infos.add(String.format("幅值系数%s%s",LABLE,String.valueOf(PerUtils.getTwoData(mSonicWave.ampRatio))));
        infos.add(String.format("剖面数%s%s",LABLE,String.valueOf(mSonicWave.sectionCount)));
        if(mSonicWave.GpsIsValid==0)
            infos.add(String.format("GPS有效性%s%s",LABLE,"有效"));
        else
            infos.add(String.format("GPS有效性%s%s",LABLE,"无效"));

        if (infos.size()==0)
            return null;
        else
        {
            String[] infoArrays = new String[infos.size()];
            for (int i = 0; i < infos.size(); i++)
            {
                infoArrays[i] = infos.get(i);
            }
            return infoArrays;
        }
    }
}
